#pragma once
#include "noncopyable.h"
#include <string>
#include <iostream>

#define LOG_INFO(LogmsgFormat, ...)                              \
    do                                                           \
    {                                                            \
        Logger &Logger = Logger::instance();                     \
        Logger.setLogLevel(INFO);                                \
        char buf[1024] = {0};                                    \
        snprintf(buf, sizeof(buf), LogmsgFormat, ##__VA_ARGS__); \
        Logger.log(buf);                                         \
    } while (0);

#define LOG_WARNING(LogmsgFormat, ...)                           \
    do                                                           \
    {                                                            \
        Logger &Logger = Logger::instance();                     \
        Logger.setLogLevel(WARNING);                             \
        char buf[1024] = {0};                                    \
        snprintf(buf, sizeof(buf), LogmsgFormat, ##__VA_ARGS__); \
        Logger.log(buf);                                         \
    } while (0);

#define LOG_DEBUG(LogmsgFormat, ...)                             \
    do                                                           \
    {                                                            \
        Logger &Logger = Logger::instance();                     \
        Logger.setLogLevel(DEBUG);                               \
        char buf[1024] = {0};                                    \
        snprintf(buf, sizeof(buf), LogmsgFormat, ##__VA_ARGS__); \
        Logger.log(buf);                                         \
    } while (0);

#ifdef MUDEBUG
#define LOG_ERROR(LogmsgFormat, ...)                             \
    do                                                           \
    {                                                            \
        Logger &logger = Logger::instance();                     \
        logger.setLogLevel(ERROR);                               \
        char buf[1024] = {0};                                    \
        snprintf(buf, sizeof(buf), LogmsgFormat, ##__VA_ARGS__); \
        logger.log(buf);                                         \
    } while (0)
#else
#define LOG_ERROR(LogmsgFormat, ...) /* 不执行任何操作 */
#endif

#define LOG_FATAL(LogmsgFormat, ...)                             \
    do                                                           \
    {                                                            \
        Logger &Logger = Logger::instance();                     \
        Logger.setLogLevel(FATAL);                               \
        char buf[1024] = {0};                                    \
        snprintf(buf, sizeof(buf), LogmsgFormat, ##__VA_ARGS__); \
        Logger.log(buf);                                         \
    } while (0);

enum level
{
    INFO,
    ERROR,
    FATAL,
    WARNING,
    DEBUG
};

// 输出日志类
class Logger :  noncopyable
{
public:
    // 获取实例
    static Logger &instance();
    // 设置日志等级
    void setLogLevel(int level);
    // 日志输出
    void log(std::string msg);

private:
    int loglevel_;
    Logger() {}
};